from core.Dao import Dao
from core.util import TimeUtil
import random
from core.po import Page

userDao = Dao('test', 'user')


def test_add():
    rowData = {'user_id': 0, 'name':'wendao', 'age': 35, 'sex': 'F', 'ctime': TimeUtil.get_timestamp()}
    start = TimeUtil.get_system_timestamp()
    for i in range(0,1000):
        try:
            rowData['user_id'] = i
            rowData['name'] = 'wendao' + str(random.randint(0,10) % 4)
            userDao.insert(rowData)
        except Exception as e:
            print(e)

    end = TimeUtil.get_system_timestamp()
    print('exec time '+ str(end - start))


def test_delete():
    delNum = userDao.delete("user_id>%s", [4])
    print(delNum)


def test_update():
    upNum = userDao.update("user_id>%s", [0], {'age':20})
    print(upNum)


def test_query():
    ret = userDao.find(['name', 'age', 'sex'], " user_id>%s ", [0], None, Page.Page(0, 10, 1))
    print(ret)


def test_stat():
    retData = userDao.sql("SELECT COUNT(*) FROM test.`user` GROUP BY `name`", list())
    print(retData)


if __name__ == '__main__':
    test_add()
    test_delete()
    test_update()
    test_query()
    test_stat()

